package com.housekeeping.housekeeping.order.wxPay.dao.impl;

import com.housekeeping.housekeeping.utils.DBManager;
import com.housekeeping.housekeeping.order.utils.PrepareStatementUtils;
import com.housekeeping.housekeeping.order.wxPay.dao.PayOrderDao;
import com.housekeeping.housekeeping.order.wxPay.entity.Account;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PayOrderDaoImpl implements PayOrderDao {

    @Override
    public int addPayOrder(long money, Account account, Long pid) throws SQLException {
        Connection conn = null;
        PreparedStatement pStmt = null;
        int count = 0;
        try {
             conn = DBManager.getConnection();
            String sql = "insert into trade_pay(pid,account,money) values(?,?,?)";
             pStmt = conn.prepareStatement(sql);
            PrepareStatementUtils.SetValueToSQL(pStmt, pid, account.getAccount(), money);
            count = pStmt.executeUpdate();
        } finally {
            DBManager.close(pStmt, conn);
        }
        return count;
    }

    @Override
    public int updatePayOrder(Long payId) throws SQLException {
        Connection conn = null;
        PreparedStatement pStmt = null;
        int count = 0;
        try {
            conn = DBManager.getConnection();
            String sql = "update trade_pay set state = 1 where pid = ?";
             pStmt = conn.prepareStatement(sql);
            PrepareStatementUtils.SetValueToSQL(pStmt, payId);
            count = pStmt.executeUpdate();
        } finally {
            DBManager.close(pStmt, conn);
        }
        return count;
    }
}
